﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace InSafetyAirServer.Model
{
    [DataContract]
    public class Path<T, E> : IComparable
    {
        private List<Edge<T, E>> _edges;
        private int _cost;
        public Path()
        {
            _edges = new List<Edge<T, E>>();
            _cost = 0;
        }

        public void AddEdge(Edge<T,E> edge, string field)
        {
            _edges.Add(edge);
            _cost += (int) edge.Data.GetType().GetProperty(field).GetValue(edge.Data, null);
        }

        [DataMember]
        public List<Edge<T, E>> Edges
        {
            get
            {
                return _edges;
            }
            set
            {
                _edges = value;
            }
        }

        [DataMember]
        public int Cost
        {
            get
            {
                return _cost;
            }

            set
            {
                _cost = value;
            }
        }

        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
            Path<T, E> otherPath = obj as Path<T, E>;
            if (otherPath != null)
                return (this.Cost > otherPath.Cost) ? 1 : -1;
            else
                throw new ArgumentException("Object is not a Path");
        }

        public Path<T, E> GetRange(int start, int end, string field)
        {
            Path<T, E> path = new Path<T, E>();
            if(end < _edges.Count)
            {
                List<Edge<T, E>> edges = _edges.GetRange(start, end);
                foreach (Edge<T, E> e in edges)
                {
                    path.AddEdge(e, field);
                }
            }
            return path;
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            Path<T, E> otherPath = obj as Path<T, E>;
            if (otherPath != null)
            {
                if (this.Cost != otherPath.Cost) return false;
                if (this.Edges.Count != otherPath.Edges.Count) return false;
                else
                {
                    for (int i = 0; i < this.Edges.Count; i++)
                    {
                        if (this.Edges[i] != otherPath.Edges[i]) return false;
                    }
                }
                return true;
            }
            else
                throw new ArgumentException("Object is not a Path");
        }

        public override int GetHashCode()
        {
            throw new NotImplementedException();
        }
    }
}
